package com.gkj.controller;


import com.gkj.blog.business.consts.CommonConst;
import com.gkj.blog.business.consts.HttpConsts;
import com.gkj.blog.business.dto.ImgUploadDto;
import com.gkj.blog.business.exception.FileException;
import com.gkj.blog.util.DateUtil;
import com.gkj.blog.util.FileUtil;
import com.gkj.blog.util.QiniuApi;
import com.gkj.blog.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class ImageController {
    private static Logger logger= Logger.getLogger(ImageController.class);
    @PostMapping("/upload/img")
    public ImgUploadDto uploadImg(MultipartFile file[], HttpServletRequest request){
        List<String> accessURLs=new LinkedList<String>();
        for(MultipartFile f:file) {
            QiniuApi qiniuApi = QiniuApi.getInstance().withFileName(f.getOriginalFilename(), "img/");
            try {
                String visitUrl = qiniuApi.upload(f.getInputStream(), f.getContentType());
                accessURLs.add(CommonConst.QINIU_BUCKET_ROOTPATH+visitUrl);
                SessionUtil.addEditImage(visitUrl);
            } catch (IOException e) {
                logger.error("上传图片错误");
                throw new FileException("上传失败");
            }
        }
        return new ImgUploadDto(0,accessURLs);
    }

}


